/*
 * Copyright 2005 Joe Walker
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.util;

import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

import org.directwebremoting.convert.BaseV20Converter;
import org.directwebremoting.dwrp.ProtocolConstants;
import org.directwebremoting.dwrp.SimpleOutboundVariable;
import org.directwebremoting.extend.Converter;
import org.directwebremoting.extend.InboundContext;
import org.directwebremoting.extend.InboundVariable;
import org.directwebremoting.extend.MarshallException;
import org.directwebremoting.extend.OutboundContext;
import org.directwebremoting.extend.OutboundVariable;
import org.directwebremoting.util.JavascriptUtil;
import org.directwebremoting.util.LocalUtil;

/**
 * An implementation of Converter for Dates.
 * @author Joe Walker [joe at eireneh dot com]
 * @version $Id: Date2StrConverter.java,v 1.3 2007/12/20 08:16:23 yejb Exp $
 */
public class Date2StrConverter extends BaseV20Converter implements Converter {

    public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException
    {
        String value = LocalUtil.decode(iv.getValue());        
        // If the text is null then the whole bean is null
        if (value.trim().equals(ProtocolConstants.INBOUND_NULL)||value.trim().equals(""))
        {
            return null;
        }

        try {
            Date date = DateUtil.convertStringToDate(value);
            if (paramType == Date.class) {
                return date;
            } else if (paramType == java.sql.Date.class) {
                return new java.sql.Date(date.getTime());
            } else if (paramType == Time.class) {
                return new Time(date.getTime());
            } else if (paramType == Timestamp.class) {
                return new Timestamp(date.getTime());
            } else if (paramType == Calendar.class) {
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                return cal;
            } else {
                throw new MarshallException(paramType);
            }
        } catch (MarshallException ex) {
            throw ex;
        } catch (Exception ex){
            throw new MarshallException(paramType, ex);
        }
    }


    public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException
    {
        if (!(data instanceof Date)) {
            throw new MarshallException(data.getClass());
        } 
        data = DateUtil.convertDateToString((Date)data);
        String escaped = JavascriptUtil.escapeJavaScript(data.toString());
        return new SimpleOutboundVariable('\"' + escaped + '\"', outctx, true);

    }
}
